/*
 * Package il.ac.biu.cs.grossmm.impl.server
 * File ComponentManagerImpl.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 */

package il.ac.biu.cs.grossmm.impl.server;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import il.ac.biu.cs.grossmm.api.server.Component;
import il.ac.biu.cs.grossmm.api.server.ComponentManager;
import il.ac.biu.cs.grossmm.api.server.Disposable;

public class ComponentManagerImpl extends Node<ComponentManagerImpl> implements
	ComponentManager {
	final WrappedImpl wrapped;

	// hint to object map
	Map<Object, Object> usedObjects = new HashMap<Object, Object>();

	protected final Component component;

	public ComponentManagerImpl(WrappedImpl wrapped)
	{
		this.wrapped = wrapped;
		this.component = (Component) wrapped.getObject();
	}

	public void initialize() throws Exception
	{
		try {
			component.setComponentManager(this);
		} catch (Exception e) {
			ServiceManagerImpl sm = wrapped.serviceManager;

			if (sm != null)
				sm.stopService(e);
			
			e.printStackTrace();

			abort(e);
			throw e;
		}
	}

	public Object getComponent(Object hint) throws Exception
	{
		// associated with object and loaded via this hint?
		Object o = usedObjects.get(hint);
		if (o != null)
			return o;

		// associated with component and loaded via this hint?
		ComponentManagerImpl cm = getByHint(hint);
		if (cm != null)
			return cm.component;

		// load
		WrappedImpl w = wrapped.load(hint);

		cm = w.getComponentManager();
		if (cm == null) {
			o = w.getObject();
			usedObjects.put(hint, o);
		} else {
			o = cm.component;
			setHint(cm, hint);
		}

		return o;
	}

	public void releaseComponent(Object hint)
	{
		if (usedObjects.remove(hint) != null)
			return;

		removeHint(hint);
	}

	/**
	 * Called when no longer in use
	 * 
	 */
	protected void unused()
	{
		dispose(null);
	}

	/**
	 * Called when contained resource is no longer in use. Both the resource and
	 * the resource manager are unloaded
	 * 
	 * @param e
	 */
	protected void dispose(Exception e)
	{
		if (component instanceof Disposable) {
			((Disposable) component).dispose(e);
		}

		// serverManager.releaseResourceManager(name);
	}

	public void abort(Exception e)
	{
		ServiceManagerImpl sm = wrapped.getServiceManager();

		if (sm != null)
			sm.terminated(e);

		discard(e);

		dispose(e);
	}

	/**
	 * Called when one of resources used by the contained resource has aborted.
	 * 
	 * @param manager
	 *            the resource manager of the aborted resource
	 * @param e
	 *            exception which provides further information
	 */
	protected void discarded(ComponentManager manager, Set<Object> hints,
		Exception e)
	{
		try {
			for (Object hint : hints) {
				component.aborted(hint, e);
			}
		} catch (Exception e2) {
			abort(e2);
		}
	}

	@SuppressWarnings("unchecked")
	public <C> C getByClass(Class<C> classHint) throws Exception
	{
		return (C) getComponent(classHint);
	}

}
